const app = getApp();
const path = app.globalData.path;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 0,
    departmentId: '',
    pullBar: false,
    roleList: [],
    userList: [],
    selected: 0
  },
  findWithAttr: function (array, attr, value) {
    for (var i = 0; i < array.length; i += 1) {
      if (array[i][attr] === value) {
        return i;
      }
    }
    return -1;
  },
 
  searchUser: function (e) { //登录/注册提交事件
    var formData = e.detail.value;
    var that = this
    console.log("test");

    var loginFlag = wx.getStorageSync('token');
    wx.request({
      url: path + 'wx-com/sys-user-department!getSysUserList.action',
      data: {
        departmentName: formData.deptName,
        userName: formData.username,
        page: 1,
        start: 0,
        limit: 25
      },
      dataType: "json",
      header: {
        'token': loginFlag,
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        that.setData({
          userList: res.data.data
        });
        console.log(res.data);
      }
    })

  },

  pullBar: function () {
    this.setData({
      pullBar: !this.data.pullBar
    })
  },

  turnPage: function (e) {
    this.setData({
      currentPage: e.currentTarget.dataset.index
    })
  },

  turnTitle: function (e) {
    if (e.detail.source == "touch") {
      this.setData({
        currentPage: e.detail.current
      })
    }
  },

  turnMenu: function (e) {
    var that = this;
    console.log(e);
    that.setData({
      departmentId: e.currentTarget.dataset.id,
      selected: e.currentTarget.dataset.index
    })
    // console.log("aaaaas");
    console.log(e.currentTarget.dataset.index);
    // 使用部门节点ID 去查询部门 人员列表
    var loginFlag = wx.getStorageSync('token');
    wx.request({
      url: path + 'wx-com/sys-user-department!getSysUserList.action',
      header: {
        'token': loginFlag
      },
      dataType: "JSON",
      data: {
        departmentId: that.data.departmentId,
        includeSubDept: true
      },
      success: function (res) {

        var result = JSON.parse(res.data);
        console.log(result.data);
        that.setData({
          userList: result.data
        })

      },
      error: function (res) {
        console.log(res);
      }
    })
  },

  selectUser: function (e) {
    var that = this;
    console.log(e);
    var info = this.data.userList;
    wx.navigateBack({
      complete: function () {
        var page = getCurrentPages();
        var pageIndex = null;
        var pageIndexCreate = that.findWithAttr(page, 'route', "pages/issue/createIssue/createIssue");
        var pageIndexDraft = that.findWithAttr(page, 'route', "pages/issue/issueList/issueDraft");
        if (pageIndexDraft >= 0) {
          var pageIndex = pageIndexDraft;
        } else {
          var pageIndex = pageIndexCreate;
        }
        page[pageIndex].setData({
          reportOrg: e.currentTarget.dataset.reportorg,
          reportByName: e.currentTarget.dataset.name,
          reportBy: e.currentTarget.dataset.id
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    var that = this;
    let loginFlag = wx.getStorageSync('token');
    // wx.request({
    //   url: path + 'wx-com/sys-user-department!getSysDepartmentGroupTree.action',
    //   header: {
    //     'token': loginFlag
    //   },
    //   dataType: "JSON",
    //   data: {
    //     node: '88856D1EB0224592AA555238A05F86FB'
    //   },
    //   success: function (res) {

    //     var result = JSON.parse(res.data);
    //     that.setData({
    //       roleList: result.data
    //     })
    //   },
    //   error: function (res) {
    //     console.log(res);
    //   }
    // })
  },

  /** 
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */

  onShow: function () {

  },

  /** 
   * 生命周期函数--监听页面隐藏
   */

  onHide: function () {

  },

  /** 
   * 生命周期函数--监听页面卸载
   */

  onUnload: function () {

  },

  /** 
   * 页面相关事件处理函数--监听用户下拉动作
   */

  onPullDownRefresh: function () {

  },

  /** 
   * 页面上拉触底事件的处理函数
   */

  onReachBottom: function () {

  },

  /** 
   * 用户点击右上角分享
   */

  onShareAppMessage: function () {

  }

})